package com.lagou.edu.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.dao.LagouUserDao;
import com.lagou.edu.service.fallback.LagouAuthCodeServiceHandlersClass;
import com.lagou.pojo.LagouToken;
import com.lagou.pojo.LagouUser;
import com.lagou.service.LagouAuthCodeService;
import com.lagou.service.LagouUserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;


@Service
public class LagouUserServiceImpl implements LagouUserService {

    @Autowired
    private LagouUserDao lagouUserDao;

    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Reference
    private LagouAuthCodeService lagouAuthCodeService;


    //0 注册成功 1 该邮箱已注册 2 注册码已过期 3 请求服务超时，熔断 4 验证码错误 5 该邮箱没有发送验证码

    @Override
    @SentinelResource(value = "findRegisterOpenState",blockHandlerClass = LagouAuthCodeServiceHandlersClass.class,
            blockHandler = "handleException",fallbackClass = LagouAuthCodeServiceHandlersClass.class,fallback = "handleError")
    public int register(String email, String password, String code) {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //是否已注册
        if(isRegistered(email)){
            return 1;
        }
        System.out.println("进入验证码远程调用服务");
        //用数据库保存的验证码和从邮箱获得的输入验证码进行比对,在10分钟内有效
        int zt = lagouAuthCodeService.validateAuthCode(email, code);
        if(zt != 0){

            return zt;
        }

        //1.保存注册信息
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        lagouUser.setCreatetime(new Date());
        lagouUser.setLogintime(new Date());
        lagouUserDao.save(lagouUser);

        //2.注册成功后，根据<用户邮箱+密码>生成token，用UUID模拟
        String tokenText = email + password;
        UUID uuid = UUID.nameUUIDFromBytes(tokenText.getBytes());

        LagouToken token = new LagouToken();
        token.setEmail(email);
        token.setToken(uuid.toString());
        lagouTokenDao.save(token);

        return zt;
    }

    @Override
    public boolean isRegistered(String email) {
        LagouUser lagouUser= lagouUserDao.findFirstByEmail(email);
        return lagouUser==null?false:true;
    }

    @Override
    public LagouToken login(String email, String password) {

        LagouUser lagouUser = lagouUserDao.findFirstByEmail(email);
        //已注册
        if (lagouUser != null && lagouUser.getPassword().equalsIgnoreCase(password)) {
            lagouUser.setLogintime(new Date());
            lagouUserDao.save(lagouUser);

            return lagouTokenDao.findFirstByEmail(email);
        }
        return null;
    }

    @Override
    public String getEmailByToken(String token) {
        LagouToken lagouToken = lagouTokenDao.findFirstByToken(token);
        return lagouToken == null ? "NOT FOUND" : lagouToken.getEmail();
    }
}
